
/**
 * @file log.h
 * @author fishmwei <fishmwei@qq.com>
 * @brief 
 *
 * @copyright
 * Copyright All Reserved
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */
#ifndef _LOG_H
#define _LOG_H

#include <stdio.h>

#include "common.h"
#include "types.h"


#define CLOG_LOG_ERR(...) clog_log(CLOG_LL_ERR, __VA_ARGS__)
#define CLOG_LOG_WRN(...) clog_log(CLOG_LL_WRN, __VA_ARGS__)
#define CLOG_LOG_INF(...) clog_log(CLOG_LL_INF, __VA_ARGS__)
#define CLOG_LOG_DBG(...) clog_log(CLOG_LL_DBG, __VA_ARGS__)

extern clog_log_level_t stderr_ll;  /**< stderr log level */

#ifdef CLOG_SYSLOG
extern clog_log_level_t syslog_ll;  /**< syslog log level */
#endif 

extern clog_log_cb log_cb;          /**< logging callback */


/**
 * @brief Log a message.
 *
 * @param[in] plugin Whether the message was generated by a plugin.
 * @param[in] ll Log level (severity).
 * @param[in] msg Message.
 */
API void clog_log_msg(int plugin, clog_log_level_t ll, const char *msg);

/**
 * @brief Log a message with variable arguments.
 *
 * @param[in] ll Log level (severity).
 * @param[in] format Message format.
 * @param[in] ... Format arguments.
 */
API void clog_log(clog_log_level_t ll, const char *format, ...);

/**
 * @brief Enables / disables / changes log level (verbosity) of logging to
 * standard error output.
 *
 * By default, logging to stderr is disabled. Setting log level to any value
 * other than ::CLOG_LL_NONE enables the logging to stderr. Setting log level
 * back to ::CLOG_LL_NONE disables the logging to stderr.
 *
 *
 * @param[in] log_level Requested log level (verbosity).
 */
void clog_log_stderr(clog_log_level_t log_level);

/**
 * @brief Learn current standard error output log level.
 *
 * @return stderr log level.
 */
clog_log_level_t clog_log_get_stderr(void);



#ifdef CLOG_SYSLOG

/**
 * @brief Enables / disables / changes log level (verbosity) of logging to system log.
 *
 * By default, logging into syslog is disabled. Setting log level to any value
 * other than ::CLOG_LL_NONE enables the logging into syslog. Setting log level
 * back to ::CLOG_LL_NONE disables the logging into syslog.
 *
 * Library messages are logged with LOG_USER facility and plugin (syrepo-plugind) messages are
 * logged with LOG_DAEMON facility.
 *
 * @note Please note that enabling logging into syslog will overwrite your syslog
 * connection settings (calls openlog), if you are connected to syslog already.
 *
 * @param[in] app_name Name of the application. If not set, "sysrepo" will be used.
 * @param[in] log_level Requested log level (verbosity).
 */
void clog_log_syslog(const char *app_name, clog_log_level_t log_level);

/**
 * @brief Learn current system log log level.
 *
 * @return syslog log level.
 */
clog_log_level_t clog_log_get_syslog(void);


#endif 

/**
 * @brief Sets callback that will be called when a log entry would be populated.
 * Callback will be called for every message __regardless__ of any log level.
 *
 * @param[in] log_callback Callback to be called when a log entry would populated.
 */
void clog_log_set_cb(clog_log_cb log_callback);

/** @} logging */


#endif
